package com.example.cursor_orm.dao.impl;

import com.example.cursor_orm.dao.RolePermissionDAO;
import com.example.cursor_orm.model.RolePermission;
import com.example.cursor_orm.util.DatabaseUtil;
import org.springframework.stereotype.Repository;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

@Repository
public class RolePermissionDAOImpl implements RolePermissionDAO {
    
    @Override
    public int insert(RolePermission rolePermission) {
        String sql = "INSERT INTO role_permission (role_id, permission_id) VALUES (?, ?)";
        return DatabaseUtil.executeUpdate(sql, rolePermission.getRoleId(), rolePermission.getPermissionId());
    }
    
    @Override
    public int deleteById(Integer id) {
        // RolePermission表没有id字段，使用复合主键删除
        String sql = "DELETE FROM role_permission WHERE role_id = ? AND permission_id = ?";
        return DatabaseUtil.executeUpdate(sql, id, id);
    }
    
    @Override
    public int update(RolePermission rolePermission) {
        // RolePermission表没有id字段，无法更新，只能删除后重新插入
        return 0;
    }
    
    @Override
    public RolePermission findById(Integer id) {
        // RolePermission表没有id字段，无法通过id查询
        return null;
    }
    
    @Override
    public List<RolePermission> findAll() {
        String sql = "SELECT * FROM role_permission ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToRolePermissionList(rs);
    }
    
    @Override
    public List<RolePermission> findByCondition(String condition) {
        String sql = "SELECT * FROM role_permission WHERE " + condition + " ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        return mapResultSetToRolePermissionList(rs);
    }
    
    @Override
    public int count() {
        String sql = "SELECT COUNT(*) FROM role_permission";
        ResultSet rs = DatabaseUtil.executeQuery(sql);
        try {
            if (rs != null && rs.next()) {
                return rs.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }
    
    @Override
    public List<RolePermission> findByRoleId(Integer roleId) {
        String sql = "SELECT * FROM role_permission WHERE role_id = ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, roleId);
        return mapResultSetToRolePermissionList(rs);
    }
    
    @Override
    public List<RolePermission> findByPermissionId(Integer permissionId) {
        String sql = "SELECT * FROM role_permission WHERE permission_id = ? ORDER BY id";
        ResultSet rs = DatabaseUtil.executeQuery(sql, permissionId);
        return mapResultSetToRolePermissionList(rs);
    }
    
    @Override
    public int assignPermissionToRole(Integer roleId, Integer permissionId) {
        String sql = "INSERT INTO role_permission (role_id, permission_id) VALUES (?, ?)";
        return DatabaseUtil.executeUpdate(sql, roleId, permissionId);
    }
    
    @Override
    public int removePermissionFromRole(Integer roleId, Integer permissionId) {
        String sql = "DELETE FROM role_permission WHERE role_id = ? AND permission_id = ?";
        return DatabaseUtil.executeUpdate(sql, roleId, permissionId);
    }
    
    @Override
    public int deleteByRoleId(Integer roleId) {
        String sql = "DELETE FROM role_permission WHERE role_id = ?";
        return DatabaseUtil.executeUpdate(sql, roleId);
    }
    
    @Override
    public int deleteByPermissionId(Integer permissionId) {
        String sql = "DELETE FROM role_permission WHERE permission_id = ?";
        return DatabaseUtil.executeUpdate(sql, permissionId);
    }
    
    @Override
    public boolean hasPermission(Integer roleId, Integer permissionId) {
        String sql = "SELECT COUNT(*) FROM role_permission WHERE role_id = ? AND permission_id = ?";
        ResultSet rs = DatabaseUtil.executeQuery(sql, roleId, permissionId);
        try {
            if (rs != null && rs.next()) {
                return rs.getInt(1) > 0;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    
    /**
     * 将ResultSet映射为RolePermission对象
     */
    private RolePermission mapResultSetToRolePermission(ResultSet rs) {
        try {
            if (rs != null && rs.next()) {
                RolePermission rolePermission = new RolePermission();
                rolePermission.setRoleId(rs.getInt("role_id"));
                rolePermission.setPermissionId(rs.getInt("permission_id"));
                return rolePermission;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    /**
     * 将ResultSet映射为RolePermission对象列表
     */
    private List<RolePermission> mapResultSetToRolePermissionList(ResultSet rs) {
        List<RolePermission> rolePermissions = new ArrayList<>();
        try {
            if (rs != null) {
                while (rs.next()) {
                    RolePermission rolePermission = new RolePermission();
                    rolePermission.setRoleId(rs.getInt("role_id"));
                    rolePermission.setPermissionId(rs.getInt("permission_id"));
                    rolePermissions.add(rolePermission);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return rolePermissions;
    }
} 